package com.example.the_breakfast_club;

import java.util.ArrayList;
import java.util.List;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.DatePicker.OnDateChangedListener;


/**
 * Screen that lists all items, 'main' screen of the entire program.
 * @author Team 19
 */
public class ResultsActivity extends Activity {
	
	private ListView lv;
	private DatabaseHandler db;
	private EditText mNameFilter;
	private CheckBox mKeepFilter;
	private CheckBox mHeirFilter;
	private CheckBox mMiscFilter;
	private DatePicker mDateFilter;
	private int dateNum;
	
	ArrayList<String> item_array = new ArrayList<String>();
	
	/**
	 * Constructor for any activity.
	 * @param savedInstanceState, retained state information
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.activity_results);
		db = DatabaseHandler.getInstance(this);
		
		mNameFilter = (EditText) findViewById(R.id.name_filter);
		mKeepFilter = (CheckBox) findViewById(R.id.checkBox1);
		mHeirFilter = (CheckBox) findViewById(R.id.checkBox2);
		mMiscFilter = (CheckBox) findViewById(R.id.checkBox3);
		mKeepFilter.setChecked(true);
		mHeirFilter.setChecked(true);
		mMiscFilter.setChecked(true);
		mDateFilter = (DatePicker) findViewById(R.id.datePicker1);
		
		lv = (ListView) findViewById(R.id.item_list);
		lv.setTextFilterEnabled(true);
		
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, item_array);
        lv.setAdapter(arrayAdapter);
        
        //generating a readable integer from the date picker
        dateNum = (mDateFilter.getYear() * 10000) + ((mDateFilter.getMonth() + 1) * 100) + mDateFilter.getDayOfMonth();
        
        //populates the list initially using an empty string so that all items are shown
        populateList("");
        
        db.checkForMatch();
        
        //called every time the date filter is changed
        mDateFilter.init(2013, 1, 1, new OnDateChangedListener() {
        	@Override
        	public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
        		dateNum = (mDateFilter.getYear() * 10000) + ((mDateFilter.getMonth() + 1) * 100) + mDateFilter.getDayOfMonth();
        		populateList(mNameFilter.getText().toString());
        		arrayAdapter.notifyDataSetChanged();
        	    Log.d("asdasd",Integer.toString(dateNum));
        	}
        });
        
        //called every time a key is pressed, to check whether the list needs to be updated to the name filter
        mNameFilter.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				Log.d("name filter","key input!");
				populateList(mNameFilter.getText().toString());
				arrayAdapter.notifyDataSetChanged();
				return false;
			}
		});
        
        //called every time keepsake check box is changed
        mKeepFilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        	   @Override
        	   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        		   populateList(mNameFilter.getText().toString());
        		   arrayAdapter.notifyDataSetChanged();
        	   }
        });
        
        //called every time heirloom check box is changed
        mHeirFilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        	   @Override
        	   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        		   populateList(mNameFilter.getText().toString());
        		   arrayAdapter.notifyDataSetChanged();
        	   }
        });
        
        //called every time miscellaneous check box is changed
        mMiscFilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        	   @Override
        	   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        		   populateList(mNameFilter.getText().toString());
        		   arrayAdapter.notifyDataSetChanged();
        	   }
        });
        
		findViewById(R.id.new_lost_item_button).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				arrayAdapter.notifyDataSetChanged();
				Intent intent = new Intent(ResultsActivity.this, NewItemActivity.class);
				ResultsActivity.this.startActivity(intent);
			}
		});
		
		//Compares all items in lost list and all 
		//items in found list. If method returns true- display message? change status
		//of lost item to found and change status of found item to returned.
		//db.checkForMatch();
		//populateList(mNameFilter.getText().toString());
		
		findViewById(R.id.view_found_button).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(), Results2Activity.class);
				startActivity(intent);
			}
		});
		
		findViewById(R.id.logout_button).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(), LoginActivity.class);
				startActivity(intent);
			}
		});
	}
	
	/**
	 * Clears the list view, then adds the items in based on filter parameters.
	 * @param nameFilter Text currently entered into name filter text field.
	 */
	public void populateList(String nameFilter){
		List<Item> allItems = db.getAllItems();
		Log.d("population","cleared");
		item_array.clear();
		Log.d("populate using","/" + nameFilter);
		for(Item each : allItems) {
			if(!mKeepFilter.isChecked() && each.getCategory() == 0){
				continue;
			}
			if(!mHeirFilter.isChecked() && each.getCategory() == 1){
				continue;
			}
			if(!mMiscFilter.isChecked() && each.getCategory() == 2){
				continue;
			}
			if(dateNum > each.getTime()){
				continue;
			}
			if(!each.getFound()){
				if(nameFilter.trim().length() == 0 || nameFilter == null){
					Log.d("characters","0");
					Log.d("add",each.getName());
					if(each.getResolved())
						item_array.add(each.getName() + " (Resolved)");
					else
						item_array.add(each.getName());
					
				}else{
					Log.d(nameFilter,each.getName());
					if(nameFilter.equals(each.getName())){
						Log.d("characters","match");
						Log.d("add",each.getName());
						if(each.getResolved())
							item_array.add(each.getName() + " (Resolved)");
						else
							item_array.add(each.getName());
					}
				}
			}
		}
	}
	
	
	/**
	 * Inflates the specified menu. 
	 * Initializes the contents of the Activity's standard options menu.
	 * @param menu, menu to be inflated
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_results, menu);
		return true;
	}
}
